package com.core;

import com.ShopData;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.SingleThreadModel;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class NormalShop extends HttpServlet implements SingleThreadModel {

    @Override
    public void init(){
        System.out.println("NormalShop init");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.print("NormalShop doPost ");
        Do(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.print("NormalShop doGet ");
        Do(request,response);
    }

    private void Do(HttpServletRequest request, HttpServletResponse response){
        ShopData.instance.normal_shop_count++;
        PrintMain(request,response);
    }

    private void PrintMain(HttpServletRequest request, HttpServletResponse response){

        String main_page = this.getServletContext().getInitParameter("mainpage");
        // 转发方式  成功跳转到mainpage页面
        RequestDispatcher d = this.getServletContext().getRequestDispatcher(main_page);
        try{
            d.forward(request,response);
        }catch(Exception e){
            System.out.print(e.getMessage());
        }
    }
}
